package Telas;

import DAO.ErroConexao;
import DAO.FabricaConexao;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import models.Login1;

public class Login extends javax.swing.JFrame {

    /**
     *
     *
     *
     * Creates new form NewJFrame
     */
    public Login() {
        initComponents();
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
        java.util.Date data = new java.util.Date();
        data.setDate(data.getDate());
        String teste = format.format(data);
        data1.setText(teste);

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jPasswordField1 = new javax.swing.JPasswordField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jSeparator5 = new javax.swing.JSeparator();
        jLabel5 = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        jLabel8 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jSeparator6 = new javax.swing.JSeparator();
        data1 = new javax.swing.JLabel();
        campus2 = new javax.swing.JButton();
        campus3 = new javax.swing.JButton();
        campus1 = new javax.swing.JButton();

        jLabel6.setText("Criado por José Valdite - Francisca Jayra - Thiarlyson Queiroz - 2013");

        jLabel7.setText(" Todos os direitos reservados.");

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 57, 255));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setIconImages(null);

        jLabel2.setText("Usuário:");

        jLabel3.setText("Senha:");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton2.setText("SAIR");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPasswordField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jPasswordField1ActionPerformed(evt);
            }
        });

        jLabel9.setBackground(new java.awt.Color(204, 204, 204));
        jLabel9.setFont(new java.awt.Font("Kristen ITC", 1, 24)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(0, 0, 204));
        jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel9.setText("Gerenciador de Bibliotecas");

        jLabel10.setBackground(new java.awt.Color(204, 204, 204));
        jLabel10.setFont(new java.awt.Font("Kristen ITC", 1, 24)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(0, 0, 204));
        jLabel10.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel10.setText("BibliOnline");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel5.setText("Entrar no Sistema");

        jLabel8.setText("Criado por José Valdite - Francisca Jayra - Thiarlyson Queiroz - 2013");

        jLabel11.setText(" Todos os direitos reservados.");

        data1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N

        campus2.setText("Entrar no Campus 2");
        campus2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campus2ActionPerformed(evt);
            }
        });

        campus3.setText("Entrar no Campus 3");
        campus3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campus3ActionPerformed(evt);
            }
        });

        campus1.setText("Entrar no Campus 1");
        campus1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campus1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator2)
                    .addComponent(jSeparator5)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel9)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(99, 99, 99)
                                        .addComponent(jLabel10))))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(25, 25, 25)
                                .addComponent(jLabel3)
                                .addGap(31, 31, 31)
                                .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addComponent(jLabel2)
                                .addGap(31, 31, 31)
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 261, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(0, 8, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jSeparator6)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(data1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel8, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                        .addGap(92, 92, 92)
                                        .addComponent(jLabel11))
                                    .addComponent(campus1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campus2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(campus3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel4)
                .addGap(20, 20, 20)
                .addComponent(jLabel9)
                .addGap(6, 6, 6)
                .addComponent(jLabel10)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(data1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(8, 8, 8)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel2))
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(5, 5, 5)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(jLabel3))
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(campus1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campus2, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(campus3, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 1, Short.MAX_VALUE))
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator6, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel11))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jPasswordField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jPasswordField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jPasswordField1ActionPerformed

    private void campus1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campus1ActionPerformed
        try {
            Connection c = FabricaConexao.getConnection();
            Statement st = c.createStatement();
            ResultSet rs = st.executeQuery("select * from login");
            while (rs.next()) {
                if (jTextField1.getText() == null ? rs.getString(2) == null : jTextField1.getText().equals(rs.getString(2))) {
                    if (jPasswordField1.getText() == null ? rs.getString(3) == null : jPasswordField1.getText().equals(rs.getString(3))) {
                        if (rs.getInt(4) == 1) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu1 principal = new Menu1();
                            principal.setVisible(true);
                            Login1 login = new Login1();
                            login.setCampus("1");
                        
                        }
                        if (rs.getInt(4) == 2) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu2 principal2 = new Menu2();
                            principal2.setVisible(true);
                        }
                        if (rs.getInt(4) == 3) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu3 principal3 = new Menu3();
                            principal3.setVisible(true);
                        }
                        dispose();
                    }
                }
            }
            c.close();
        } catch (ErroConexao ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            System.out.println("erro no banco");
            System.out.println(e.getMessage());
        }

    }//GEN-LAST:event_campus1ActionPerformed

    private void campus2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campus2ActionPerformed
        try {
            Connection c = FabricaConexao.getConnection();
            Statement st = c.createStatement();
            ResultSet rs = st.executeQuery("select * from login");
            while (rs.next()) {
                if (jTextField1.getText() == null ? rs.getString(2) == null : jTextField1.getText().equals(rs.getString(2))) {
                    if (jPasswordField1.getText() == null ? rs.getString(3) == null : jPasswordField1.getText().equals(rs.getString(3))) {
                        if (rs.getInt(4) == 1) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu1 principal = new Menu1();
                            principal.setVisible(true);
                        }
                        if (rs.getInt(4) == 2) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu2 principal2 = new Menu2();
                            principal2.setVisible(true);
                        }
                        if (rs.getInt(4) == 3) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu3 principal3 = new Menu3();
                            principal3.setVisible(true);
                        }
                        dispose();
                    }
                }
            }
            c.close();
        } catch (ErroConexao ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            System.out.println("erro no banco");
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_campus2ActionPerformed

    private void campus3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campus3ActionPerformed
        try {
            Connection c = FabricaConexao.getConnection();
            Statement st = c.createStatement();
            ResultSet rs = st.executeQuery("select * from login");
            while (rs.next()) {
                if (jTextField1.getText() == null ? rs.getString(2) == null : jTextField1.getText().equals(rs.getString(2))) {
                    if (jPasswordField1.getText() == null ? rs.getString(3) == null : jPasswordField1.getText().equals(rs.getString(3))) {
                        if (rs.getInt(4) == 1) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu1 principal = new Menu1();
                            principal.setVisible(true);
                        }
                        if (rs.getInt(4) == 2) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu2 principal2 = new Menu2();
                            principal2.setVisible(true);
                        }
                        if (rs.getInt(4) == 3) {
                            JOptionPane.showMessageDialog(null, "LOGIN EFETUADO COM SUCESSO!!!" + rs.getString(2));
                            Menu3 principal3 = new Menu3();
                            principal3.setVisible(true);
                        }
                        dispose();
                    }
                }
            }
            c.close();
        } catch (ErroConexao ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException e) {
            System.out.println("erro no banco");
            System.out.println(e.getMessage());
        }
    }//GEN-LAST:event_campus3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton campus1;
    private javax.swing.JButton campus2;
    private javax.swing.JButton campus3;
    private javax.swing.JLabel data1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JSeparator jSeparator6;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
